<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <Header :todos="todos" @addTodos="addTodos" />
        <List :todos="todos" />
        <Footer :todos="todos" 
        :Allcheck="Allcheck" 
        :AllList="AllList" 
        :AllChecked="AllChecked"/>
      </div>
    </div>
  </div>
</template>

<script>
import Header from "./components/Header";
import Footer from "./components/Footer";
import List from "./components/List";
export default {
  name: "App",
  components: {
    Header,
    Footer,
    List,
  },
  data() {
    return {
      todos: JSON.parse(localStorage.getItem("list")) || [],
    };
  },
  methods: {
    addTodos(newObj) {
      let result = this.todos.find((todo) => todo.title === newObj.title);
      if (result) {
        alert("不能输入相同的值,请重新输入");
        return false;
      } else {
        this.todos.unshift(newObj);
        return true;
      }
    },
    deleteTodos(id) {
      const newTodos = this.todos.filter((todo) => todo.id !== id);
      this.todos = newTodos;
    },
    checkedDone(id, done) {
      this.todos.forEach((todo) => {
        if (todo.id === id) {
          todo.done = done;
        }
      });
    },
    deleteTodo() {
      this.todos = this.todos.filter((todo) => todo.done !== true);
    },
    checkedAll(done){
        this.todos.forEach(todo=>{
          todo.done = done
        })
    }
  },
  watch: {
    todos: {
      deep: true,
      handler(newvalue) {
        localStorage.setItem("list", JSON.stringify(newvalue));
      },
    },
  },
  // 计算属性
  computed: {
    Allcheck() {
      return this.todos.filter((todo) => todo.done === true).length;
    },
    AllList() {
      return this.todos.length;
    },
    AllChecked(){
      if (this.Allcheck == this.AllList) {
        return true;
      } else {
        return false;
      }
    }
  },
  mounted() {
    this.$bus.$on("deleteTodos", this.deleteTodos);
    this.$bus.$on("checkedDone", this.checkedDone);
    this.$bus.$on("deleteTodo", this.deleteTodo);
    this.$bus.$on("AllChecked", this.AllChecked);
    this.$bus.$on("checkedAll",this.checkedAll);
  },
  beforeDestroy(){
    this.$bus.$off('deleteTodos'),
    this.$bus.$off('checkedDone'),
    this.$bus.$off('deleteTodo'),
    this.$bus.$off('AllChecked'),
    this.$bus.$off('checkedAll')
  }
};
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>